package music.Share;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

class MyFriendAdapter extends BaseAdapter implements OnClickListener, SwipeInterface {

	private final Activity activity;
	private final LayoutInflater mInflater;
	private final ArrayList<String> friends;
	private ActivitySwipeDetector swipe;
	private String freund;
	private String aufrufer;
	private String uid;

	public MyFriendAdapter(Activity act, ArrayList<String> friends, String aufrufer, String uid) {
		this.activity = act;
		mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.friends=friends;
		this.aufrufer=aufrufer;
		swipe = new ActivitySwipeDetector(this);
		this.uid=uid;
	}
	
	public int getCount() {
		return friends.size();
	}
	
	public String getItem(int position) {
		return friends.get(position);
	}
	
	public long getItemId(int position) {
		return (long) position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		FrameLayout itemView = (FrameLayout) mInflater.inflate(R.layout.friends_item, parent, false);
		itemView.setOnTouchListener(swipe);
		itemView.setOnClickListener(this);
		final int pos = position; 
		
		// neuer onclick listener da die button id (234346806irgendwas) sonnst probleme macht in der anderen ontouch
		((Button) itemView.findViewById(R.id.loeschen)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.loeschen :
					System.out.println("Button hit! Name: " + friends.get(pos)); 			
					Methoden.DelFriend(uid, friends.get(pos));
					
					activity.startActivity(activity.getIntent()); activity.finish();
				}						
			}
		});
		bindView(itemView, position);
		
		return itemView;
	}
	private void bindView(FrameLayout itemView, int position) {
		freund = getItem(position);
		itemView.setId((int) getItemId(position));
		
		((TextView) itemView.findViewById(android.R.id.text1)).setText(freund);	
	}
	
	
	@Override
	public void bottom2top(View v) {
//		System.out.println("bottom2top");
	}
	@Override
	public void left2right(View v) {
//		System.out.println("l2r");
	}
	
	@Override
	public void right2left(View v) {
		((Button) v.findViewById(R.id.loeschen)).setVisibility(View.VISIBLE);
//		System.out.println("r2l" + v.getId());
	}
	@Override
	public void top2bottom(View v) {
//		System.out.println("top2bottom");
	}
	@Override
	public void onClick(View v) {
		System.out.println(((TextView) v.findViewById(android.R.id.text1)).getText());
		if (aufrufer.equals("sendfile")) {
			((EditText) activity.findViewById(R.id.rcvidedit)).setText(((TextView) v.findViewById(android.R.id.text1)).getText());
		}

	}
	
	
	
}